<template>
  <div>
    <el-dialog title="添加其他工作信息" :visible.sync="dialogFormVisible">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>园地名称</span>
        </div>
        <el-form
        :model="form"
        v-loading="fetchLoading"
        class="sectionForm"
        size="mini"
        element-loading-text="数据加载中,请耐心等待..."
        label-width="80px"
        label-position="right">
          <el-row :gutter="10">
            <el-col :span="12">
              <el-form-item label="园地名称">
                  <el-select v-model="form.gardenPlot" placeholder="园地名称" style="width:100%"  value-key="gardenId">
                    <el-option v-for="item in gardenPlotList"
                    :key="item.value"
                    :label="item.gardenName"
                    :value="item"
                    ></el-option>
                  </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="园地面积">
                  <el-input v-model="form.gardenPlot.gardenArea" disabled placeholder="园地面积" ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="12">
              <el-form-item label="种植品种">
                  <el-input v-model="form.gardenPlot.gardenVar" disabled placeholder="种植品种"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="果树数量">
                  <el-input v-model="form.gardenPlot.gardenSnum" disabled placeholder="果树数量"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>工作信息</span>
        </div>
        <el-form
        :model="form"
        v-loading="fetchLoading"
        class="sectionForm"
        size="mini"
        element-loading-text="数据加载中,请耐心等待..."
        label-width="80px"
        label-position="right">
          <el-row :gutter="10">
            <el-col :span="12">
              <el-form-item label="工作名称">
                  <el-input v-model="form.workName" placeholder="工作名称"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="工作内容">
                  <el-input v-model="form.workContent" placeholder="工作内容"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="12">
              <el-form-item label="开始日期">
                <el-date-picker v-model="form.beginDate" type="datetime" placeholder="开始日期" style="width:100%"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="结束日期">
                <el-date-picker v-model="form.endDate" type="datetime" placeholder="结束日期" style="width:100%"></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="12">
              <el-form-item label="工作天数">
                  <el-input v-model="form.workDay" placeholder="工作天数"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="所需费用">
                  <el-input v-model="form.workPrice" placeholder="所需费用"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="24">
              <el-form-item label="备注">
              <el-input type="textarea" v-model="form.remark"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="type==='add'" @click="add">确认添加</el-button>
        <el-button type="primary" v-else @click="add">确认修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default{
  name:'add',
  props:{
    type:{
      type:String,
    }
  },
  data(){
    return {
      dialogFormVisible:false,
      fetchLoading: false,
      form: {
        gardenPlot: {
          gardenName: '',
          gardenArea: '',
          gardenVar: '',
          gardenSnum: '',
        },
        workName: '',
        workContent: '',
        beginDate: '',
        endDate: '',
        workDay: '',
        workPrice: '',
        remark: '',
      },
      gardenPlotList: [],
    }
  },
  async created(){
    let res=await this.$API.gardenInfo.getList({})
    if(res.code==200){
      this.gardenPlotList = res.data
    }
  },
  methods:{
    // change(){
    //   if(this.form.ferQuantity > this.form.plantFood.count){
    //      return this.$message({type:"error",message:"余存数量不足"})
    //   }
    //   this.form.ferTotalPrice = this.form.ferQuantity * this.form.plantFood.plantUnitPrice 
    // },
    cancel(){
      this.dialogFormVisible = false
      Object.assign(this.$data.form, this.$options.data().form)
    },
    async add(){
      this.dialogFormVisible = false
      // this.$emit("save",)
      try{
        let res=await this.$API.otherWorkInfo.saveOrUpdate(this.form)
        if(res.code==200){
          this.$message({type:"success",message:this.type==="add"?"添加成功":"修改成功"})
          this.$parent.updatePageList()
          Object.assign(this.$data.form, this.$options.data().form)
        }
      }catch{
        this.$message({type:"error",message:this.type==="add"?"添加失败":"修改失败"})
      }
    }
  }
}
</script>

<style scoped>
.text {
    font-size: 14px;
  }

  .item {
    margin-bottom: 18px;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }
  .clearfix:after {
    clear: both
  }

  .box-card {
    width: 100%;
    margin-bottom: 20px;
  }
</style>